package com.tjetc;

import com.tjetc.entity.Bank;
import com.tjetc.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @ author: gaoyongxiang
 * @ date: 2025/8/15
 * @ version: v1.0
 */
@SuppressWarnings("all")
public class _8DbUtils {
    private static Connection connection = null;
    private static ResultSet resultSet = null;
    private static Statement statement = null;
    /*
       DButil工具的使用

     */
    // 每个测试方法执行前都要执行一次
    @BeforeAll
    public static void before() throws SQLException {
        connection = JdbcUtil.getConnection();
    }


    @Test
    public void testDML() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "insert into bank (name, money) values(?, ?)";
        int row = queryRunner.update(connection, sql, "zhaoliu", 1);
        System.out.println(row);
    }


    @Test
    public void testQuery() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from bank ";
        /*
            // 执行sql对象
            Statement stmt = null;
            // 结果集对象
            ResultSet resultSet = null;
            // 结果对象
            T result = null;

            try {
                // 判断参数是否为null 参数的长度是否>0
                if (params != null && params.length > 0) {
                    // 如果不等于null 创建PreparedStatement对象 做预编译
                    PreparedStatement ps = this.prepareStatement(conn, sql);
                    // 将stmt 赋值PreparedStatement
                    stmt = ps;
                    // 把参数赋值给？
                    this.fillStatement(ps, params);
                    // 执行sql获取结果集对象
                    resultSet = this.wrap(ps.executeQuery());
                } else {
                    // 使用Statement对象
                    stmt = conn.createStatement();
                    // 执行sql获取结果集对象
                    resultSet = this.wrap(stmt.executeQuery(sql));
                }
                // 将结果集对象给到对应的处理器
                // 底层就是通过反射将
                result = (T)rsh.handle(resultSet);
            } catch (SQLException e) {
                this.rethrow(e, sql, params);
            } finally {
                this.closeQuietly(resultSet);
                this.closeQuietly(stmt);
            }

            return result;

         */
        List<Bank> bankList = queryRunner.query(connection, sql, new BeanListHandler<>(Bank.class));
        System.out.println(bankList);
    }




    // 测试查询单行单列值
    @Test
    public void testScalarHandler() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select count(*) from bank";
        Object query = queryRunner.query(connection, sql, new ScalarHandler<>());
        System.out.println("query.getClass() = " + query.getClass());
        long count = (long) query;
        System.out.println(count);
    }



    // 每个方法执行完毕后 都要执行一次
    @AfterAll
    public static void after() {
        JdbcUtil.close(resultSet,statement, connection);
    }
}